package com.flong.selenium;


import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

@SuppressWarnings("all")
public class NuoMiTest1 {

	//public static ChromeDriver driver;
	
	//public static InternetExplorerDriver driver;
	public static WebDriver driver ;
	
    static {
    	//http://blog.csdn.net/aerchi/article/details/8055913
    	//设置浏览器启动环境变量
    	//System.setProperty("webdriver.chrome.driver", "D:\\DevSoft\\chromedriver.exe");
    	System.setProperty("webdriver.ie.driver", "d:/Devsoft/IEDriverServer.exe");
    	//driver = new ChromeDriver();
 	   DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
       ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
       driver = new InternetExplorerDriver(ieCapabilities); //问题3解决方法
       driver.manage().timeouts().implicitlyWait(1000, TimeUnit.SECONDS);//SECONDS是秒的意思
    }
    

    

    public static void main(String[] args) throws IOException, Exception {
    	
    	//String searchUrl = "https://xt.nuomi.com/search/"+URLEncoder.encode("养生保健")+"/1/0/all-0-3484-0-pd-0-0?&rid="+getUUID();
    	
    	String searchUrl ="https://suizhou.nuomi.com/search?k="+URLEncoder.encode("养生保健");
    	driver.get(searchUrl);
    	//Thread.sleep(2000);
    	WebElement html = driver.findElement(By.tagName("html"));
    	WebElement goodEl = html.findElement(By.id("j-search-goods-area"));
    	List<WebElement> rf_jcs = goodEl.findElements(By.className("row-first-item"));
    	String resultUrl = "";
    	for(WebElement rf:rf_jcs){
    		 String dealHref = rf.findElement(By.className("contentbox")).findElement(By.tagName("a")).getAttribute("href");
    		resultUrl+=dealHref+"\n";
    	}
    	
    	List<WebElement> jcs = goodEl.findElements(By.className("j-card"));
    	for(WebElement rf:jcs){
    		 String dealHref = rf.findElement(By.className("contentbox")).findElement(By.tagName("a")).getAttribute("href");
    		 resultUrl+=dealHref+"\n";
    	}
    	
    	System.out.println(resultUrl);
    	
    	
    	/*WebElement name = driver.findElement(By.className("branch-name"));
    	WebElement tel = driver.findElement(By.className("branch-tel"));
    	WebElement address = driver.findElement(By.className("branch-address"));
       */

    
    
           //driver.navigate().refresh();
           driver.close();
           driver.quit();
    }
    /**  
     * 自动生成32位的UUid，对应数据库的主键id进行插入用。  
     * @return  
     */  
    public static String getUUID() {  
        /*UUID uuid = UUID.randomUUID();  
        String str = uuid.toString();  
        // 去掉"-"符号  
        String temp = str.substring(0, 8) + str.substring(9, 13)  
                + str.substring(14, 18) + str.substring(19, 23)  
                + str.substring(24);  
        return temp;*/  
          
        return UUID.randomUUID().toString().replace("-", "");  
    }  
    
}